package com.situ.company.controller;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.situ.company.entity.Employee;
import com.situ.company.util.JDBCUtil;
import com.situ.company.util.JSONResult;
import com.situ.company.util.JSONUtil;

import sun.rmi.log.LogOutputStream;

@WebServlet("/loginServlet")
public class LoginServlet extends HttpServlet{

	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String method = req.getParameter("method");
		switch (method) {
		case "login":
			login(req, resp);
			break;
		case "logout":
			logout(req, resp);
			break;
		default:
			break;
		}
	}

	private void logout(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		HttpSession session = req.getSession();
		Employee employee = (Employee) session.getAttribute("employee");
		if (employee != null) {
			// 销毁Session
			session.invalidate();
			// 重定向到登录界面
			resp.sendRedirect(req.getContextPath() + "/web/login.jsp");
		}
	}

	private void login(HttpServletRequest req, HttpServletResponse resp) {
		String loginCode = req.getParameter("code");
		String loginPass = req.getParameter("pass");
		
		Connection connection = null;
		PreparedStatement preparedStatement = null;
		ResultSet resultSet = null;
		Employee employee = null;
		// 加载驱动
		try { // DBUtils
			connection = JDBCUtil.getConnection();
			String sql = "select id,code,name,pass,code_dept from employee where code=? and pass=?";
			preparedStatement = connection.prepareStatement(sql);
			preparedStatement.setString(1, loginCode);
			preparedStatement.setString(2, loginPass);
			resultSet = preparedStatement.executeQuery();
			System.out.println(preparedStatement);
			// 判断下一行有没有，有返回true，并指向这一行，没有返回false
			while (resultSet.next()) {
				int id = resultSet.getInt("id");
				String code = resultSet.getString("code");
				String name = resultSet.getString("name");
				String pass = resultSet.getString("pass");
				String codeDept = resultSet.getString("code_dept");
				employee = new Employee(id, code, name, pass, codeDept);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			JDBCUtil.close(connection, preparedStatement, resultSet);
		}
		
		if (employee != null) {
			// 登录成功，应该把这个用户的信息放到Session，然后Filter里面就是根据有没有用户信息
			// 判断是不是已经登录
			HttpSession session = req.getSession();
			session.setAttribute("employee", employee);
			
			JSONResult jsonResult = JSONResult.success("登录成功");
			JSONUtil.obj2Json(jsonResult, resp);
		} else {
			JSONResult jsonResult = JSONResult.error("登录失败");
			JSONUtil.obj2Json(jsonResult, resp);
		}
	}
	
	
	
}
